# running time in minutes
run_minutes <- 26.34
# running time in seconds
run_seconds <- run_minutes*60
# print result
run_seconds1 Introduction to the pre-sessional for R
The pre-sessional for R is split into two stages. First, is this self-paced webpage tutorial, second is in-person workshops which are detailed in Section 7.1.
This webpage tutorial contains instructions on how to install R on your laptop and some exercises to get you started with R. This tutorial must be completed before you join the in-person R pre-sessional workshops run by the Digital Skills Lab (DSL).
This tutorial and follow up in-person R pre-sessional workshops are part of your pre-sessional programme. They are designed to help you get up to speed with R which you will be using in your modules. Once you have completed this tutorial, see Section 7 for information on what happens next.
1.1 What do I need to do and who is this pre-sessional for?
Use this self-paced tutorial to get R and RStudio installed, and get to grips with loading and using R scripts. This should take around 45 minutes to 1 hour to complete
Come to the in-person workshops, and complete 7 notebooks on Fundamentals in R (DSI Students should complete the full 10 notebooks). This will take approximately 7 hours for the Fundamentals materials, and 10 hours for the full series so we recommend you book and attend 3-5 sessions: follow this link to book
- To save time at the start of the workshops, have a go at accessing the materials. See Section 8 for information
This tutorial is for all students who will be taking the following modules. It is strongly advised that you run through this self-paced webpage tutorial:
- MY360/361
- MY451A
- MY452A
- MY464
- MY470
- MY472
- MY452/552
- MY455/55/MY472
- MY457/557
- MY474/574
- MY461/561
- MY459/559
- DS105
- DS202
If you are on the following modules, it is advised that you come to the pre-sessional workshops, detailed in Section 7.1, to get a better understanding of R:
- MY452/552
- MY455/555
- MY472
- MY457/557
- MY474/574
- MY461/561
- MY459/559
- DS105
- DS202
1.2 Tutorial Learning Outcomes
After having completed this tutorial, you will be able to:
- Install R and RStudio
- Run R code in the console
- Open and run R scripts
- Write code in R scripts
This tutorial is a step-by-step guide to get you started. Try not to skip any of the sections. You can use the table of contents on the left hand side to help you navigate sections.
If you struggle with any of the steps from this tutorial, we recommend the following:
- Come to the in-person R pre-sessional, as detailed in Section 7.1, to get help and learn more about R. See Section 7.2 for how to book
- Seek support from our online 1-2-1 or drop-in advice, as detailed in the Section 7.3 section
2 Installing R and RStudio
The first stage of this tutorial is getting R and RStudio installed on your laptop so you can start using R! Below are step by step instructions for getting the software needed installed.
2.1 Windows install
2.1.1 Install R
To install R, you need to download the installer from the R website
Click on either base or install R for the first time
Click on the download R for Windows link
Once downloaded, open the
.exefile and follow the installation instructions on your computer
2.1.2 Install RStudio
To install RStudio we download it from the Posit website
Click on the Download RStudio Desktop link
Once downloaded, open the
.exefile and follow the installation instructions on your computer
2.2 Mac install
2.2.1 Install R
To install R on your Mac you need to know the type of processor your Mac uses. This is straightforward to find out:
- On the top navigation bar on your Mac, click on the apple icon
- From the drop down menu, select About This Mac
- In Overview you will find the information about your Mac. If you have an Intel Mac, you will see the processor row, which has information that includes Intel. If you have an M1 or M2 Mac, you will see chip and M1/M2 in the Overview with something like Chip Apple M1
2.2.1.1 M1 or M2 Mac
To install R, you need to download the installer from the R website
If you have a M1 Mac you will need click on the link the contains arm64 to download R. It will look something like
R-4.3.1-arm64.pkgOnce downloaded, open the
.pkgfile and follow the installation instructions
2.2.1.2 Intel Mac
To install R, you need to download the installer from the R website
If you have a Intel Mac you will need to click on the link that just contains the version of R. It will look something like
R-4.3.1.pkg, and can be located a touch further down the page under the header Binaries for legacy macOS/OS X systems:Once downloaded, open the
.pkgfile and follow the installation instructions
2.2.2 Install XQuartz
To run R on a Mac operating system, XQuartz is required. You can install it by following this link, downloading it and following the installation instructions.
2.2.3 Install RStudio
To install RStudio we download it from the Posit website
Click on the Download RStudio Desktop link
Once downloaded, open the
.dmgfile and follow the installation instructions on your computer
2.3 Installing R and RStudio installation issues
If your installation for R and RStudio did not work, this is likely because your computer is running an older operating system. In these cases you will have to install an older version of the software.
It will be simplest, if you have faced installation issues, for you to come to a R pre-sessional workshop for support. See Section 7.2 for more information on how to book.
3 Why R?
LSE Methodology courses primarily use R. This is because R is a excellent tool for:
- Statistics
- Data handling (i.e. cleaning and manipulating data)
- Visualisations, interactive graphics, and dashboards
- Reporting (i.e. academic writing as well as building websites)
R is an open-source tool, which means you do not need to buy a licence in order to use it, and is a popular programming language, as shown in the PYPL index from 2023
Some cool things you can do in R:
This webpage was built using R. If you are interested, come to the pre-sessional workshops and chat to one of the instructors about it!
4 First steps with R
If you have not done so already, open RStudio!
Windows users:
- Type
RStudioin the search bar (bottom left, next to windows symbol) - If you prefer, open the start menu (click windows symbol), then scroll until you find RStudio
Mac users:
- Hit command + space and type in
RStudio - If you prefer to look for RStudio in an applications folder (open finder and select Applications on the left panel), you should be looking for this icon:
When RStudio opens you should see a layout with 3 panels, similar to the image below.
The largest panel on the left with the > is the console. On the bottom right there is the files/plot panel, and top right is the environment panel.
If your installation of R and RStudio has worked, first RStudio should be open, and second you should see a message in your console panel telling you the version of R you have installed, like the image below.
There are three ways of running R code: console, scripts and R Markdown. In this tutorial we will cover the console and scripts.
The best way to get comfortable with a software is to start using it! We will run through series of exercises which will help you get more comfortable writing and running R code. The exercises include:
- Performing some calculations using R
- Convert your height from centimetres to feet and metres
- Calculate body mass index (BMI) and waist-to-hip ratio
If you get stuck on the exercises, or would prefer to follow along with a video to solve the exercises, see Section 6.
4.1 Exercise 1 - Running code from the console
The first thing we want to try is to run code from the console and see what happens. To run code from the console you type the code and press enter. Remember the console has the > symbol, and is the bottom left panel.
In RStudio, in the console, try the following calculations:
- Sum of 5 and 14
- Divide 9.6 by 1.6
- 12 minus 4 to the power of 3 Hint: use brackets
(x-y)^z - Divide 22 by 36 and multiply the result by 100
- The remainder of 55 divided by 2
The output for each question should be:
- [1] 19
- [1] 6
- [1] 512
- [1] 61.1111111111111
- [1] 1
| Operator | Symbol |
|---|---|
| Plus | + |
| Divide | / |
| Subtract | - |
| Multiply | * |
| Power | ^ |
| Remainder (modulus) | %% |
4.2 Exercise 2 - Assigning variables (still in the console)
In R, when we want to keep data and re-use it later, we assign that data to a name. There are two ways of doing this. We can use the arrow like <- or the = symbol; the arrow is most commonly used in R.
If we wanted to assign our numbers from one of our previous calculations and use them for a new calculation we would do: a <- 9.6 and b <- 1.6, then a / b. In this example, a and b are called variables.
In your console:
- Assign 5 to
a - Assign 14 to
b - Calculate the sum of
aandb, and assign the result toc - Type
cand hit enter in your console. What happened?
In your environment panel, you should see:
4.3 Exercise 3 - Making a new R script
Scripts are a useful way of remembering what we have done previously, allowing us to save code and share it with others.
In this exercise we are going to open a script, and save it.
- We have a few options to open a script, the simplest is to use a shortcut:
shift + command/ctrl + N. You can also open it manually by going the top left corner of RStudio, you should see a paper icon with a plus symbol. Click on it and select R Script - Now the script is open, save the script as something like
r-pre-sessional.R. There are a few ways of doing this, pressingcommand/ctrl + sis the simplest method - Lets try and run some code. In this example, you want to convert your running time in minutes to seconds. In the script, type or copy the following code:
- Now run the code! There are two main ways of doing this. First, we can highlight the code, and click the
Runbutton near the top centre right of RStudio. Second, we can put our cursor on each line and usecommand/ctrl + enterto run the code line by line. - The result of your code will appear in the console and should look like: [1] 1580.4
We will write more code in this script in the following exercise!
In the code example, we have hash tags (#). These are comments, which allow us to write non-code information. This is helpful to document what each step is doing.
4.4 Exercise 4 - Height metrics conversion
In this exercise we are going to convert your height from centimetres to feet and meters. Below are some formulas to help you make the calculations in the exercises.
centimetres to feet: \(heightcm \times 0.0328084\)
centimetres to meters: \(heightcm \div 100\)
feet to meters: \(heightft \times 0.3048\)
In the script we made in exercise 3, try the following exercises:
- Make a variable called
my_height, and assign 195 as the height - Convert the value of
my_heightfrom centimetres to feet. Make a new variable calledmy_height_ft, and assign the calculation ofmy_heightfrom centimetres to feet - Print your
my_height_ftvariable - Convert the value of
my_heightfrom centimetres to meters. Make a new variable calledmy_height_m, and assign the calculation ofmy_heightfrom centimetres to meters - Print your
my_height_mvariable - Convert the value of
my_height_ftfrom feet to meters. Make a new variable calledmy_height_ft_m, and assign the calculation ofmy_height_ftfrom feet to meters - Print your
my_height_ft_mvariable - Now try and do the calculations for your own height, or an estimate if you are not sure! You can just change the value of
my_height
Using a value of my_height as 195cm, we would expect to get the outputs of:
- centimetres to feet: [1] 6.397638
- centimetres to meters: [1] 1.95
- feet to meters: [1] 1.9500000624
When we say “print a value”, which is to display the output of an object, we mean for you to type a variable and run it, such as typing my_height and pressing command/ctrl + enter to view the result in the console
Remember, if you get stuck, try watching the video solution in Section 6.
4.5 Exercise 5 - Loading R scripts
In the next two exercises we will be loading a pre-prepared script and doing some coding with it.
- Click on the Download R file button and save the file where you saved your other R script
- Now open the file into R. You should be able to use the file menu to achieve this:
File > Open File... - Within the file you should see some pre-written code. Run all the code
- What output appeared in the console? Assuming this person is a man, what would their health risk be according to the below table?
| Health risk | Women | Men |
|---|---|---|
| low | 0.80 or lower | 0.95 or lower |
| moderate | 0.81-0.85 | 0.96-1.0 |
| high | 0.86 or higher | 1.0 or higher |
In the next exercise, you will do another health related calculation, working out how to calculate body mass index (BMI)
The waist-to-hip ratio metric is another measure of health that is designed to look for people at higher risk of conditions like heart disease or type 2 diabetes.
4.6 Exercise 6 - Body mass index (BMI) calculation
Use R to work out the body mass index (BMI) of someone who is 94kg, and 1.95m tall.
In the script we loaded in exercise 5:
- Assign the variables of weight and height
- Assign the variable of BMI, and calculate the BMI based of the weight and height variables
- Print the outcome
- Add comments on what each line of code is doing
You can find the formula for BMI on the British Heart Foundation website
You should get an output of [1] 24.7205785667324
The height in this calculation should be in meters. For example, someone who is 170cm tall, would be 1.70m tall in meters.
5 Note on R and RStudio
You might be asking yourself, why have I installed R and RStudio? Or why am I using RStudio and not just R?
An abridged answer to this question is R is the language we will be using, and RStudio is the environment in which we will be using R.
The unabridged answer is that R is a computer language, which means it cannot be opened like other computer applications such as an internet explorer (Chrome or Firefox) or Microsoft Word. Instead, to use R you write commands in the R language and ask your computer to interpret them. Previously, this would involve typing commands into a terminal application, or writing a script in a text editing software and running it through a terminal application. This is not the easiest way of writing R code, and is where RStudio comes in!
RStudio is an application, like Google Chrome or Firefox, which means it is easy to open on your computer. RStudio is a popular tool for using R, as it provides a pleasant interface for you to use R, with helpful features like the auto-completion, file management, and an environment panel to show/explore your data.
The terminal is a program that you use to type in commands that are then executed by your computer’s operating system. It is a text input/output environment.
6 Video solutions of exercises
To help with the exercises, we have a series of short video solutions made by the author of this tutorial.
You can either use these videos to follow along, or as a tool for finding the solutions to the exercises.